package com.bankcomm.ui.specialwidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SectionIndexer;

import com.bankcomm.R;
import com.bankcomm.ui.adapter.NameAdapter;
import com.rytong.tools.utils.Utils;

public class SideBar extends LinearLayout {
	public char[] l;
	private SectionIndexer sectionIndexter = null;
	private ListView list;

	public SideBar(Context context) {
		super(context);
		init();
		setBackgroundResource(R.color.transparent);
	}

	public SideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		setBackgroundResource(R.color.transparent);
		//setBackgroundResource(R.drawable.scrollbar_bg);
	}

	private void init() {

		l = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
				'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
				'X', 'Y', 'Z' };
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		ViewGroup vf = (ViewGroup) getParent();
		ViewGroup vg = (ViewGroup) (vf.getParent());
		if (vg == null) {
			return super.onInterceptTouchEvent(ev);
		} else {
			if (ev.getAction() == MotionEvent.ACTION_DOWN) {
				// 将父scrollview的滚动事件拦截
				vg.requestDisallowInterceptTouchEvent(true);
			} else if (ev.getAction() == MotionEvent.ACTION_UP) {
				// 把滚动事件恢复给父Scrollview
				vg.requestDisallowInterceptTouchEvent(false);
			}
		}
		return super.onInterceptTouchEvent(ev);
	}

	public void setListView(ListView _list) {
		list = _list;
		BaseAdapter ad = (BaseAdapter) _list.getAdapter();
		sectionIndexter = (SectionIndexer) ad;

	}

	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		int i = (int) event.getY();
		int idx = i / (getMeasuredHeight() / l.length);
		if (idx >= l.length) {
			idx = l.length - 1;
		} else if (idx < 0) {
			idx = 0;
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN
				|| event.getAction() == MotionEvent.ACTION_MOVE) {
			if (sectionIndexter == null) {
				sectionIndexter = (SectionIndexer) list.getAdapter();
			}
			int position = sectionIndexter.getPositionForSection(l[idx]);

			if (position == -1) {
				return true;
			}
			list.setSelection(position);
		}
		return true;
	}

	Paint paint = new Paint();

	protected void onDraw(Canvas canvas) {
		paint.setColor(Color.BLUE);
		paint.setTextSize(Utils.getScaledValueX(13));
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);
		paint.setTextAlign(Paint.Align.CENTER);
		float widthCenter = getMeasuredWidth() / 2;
		if (l.length > 0) {
			float height = getMeasuredHeight() / l.length;
			for (int i = 0; i < l.length; i++) {
				canvas.drawText(String.valueOf(l[i]), widthCenter, (i + 1)
						* height+1, paint);
			}
		}
		this.invalidate();
		super.onDraw(canvas);
	}
}